package com.gitee.feizns.quickstart.ding.talk.api.top.api.v2.department;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiV2DepartmentListsubidRequest;
import com.dingtalk.api.response.OapiV2DepartmentListsubResponse;
import com.dingtalk.api.response.OapiV2DepartmentListsubidResponse;
import com.gitee.feizns.quickstart.ding.talk.utils.Utils;
import lombok.SneakyThrows;

import java.util.List;

/**
 * 获取子部门编号（/topapi/v2/department/listsubid）
 * 接口路径：<a href="https://open.dingtalk.com/document/orgapp/obtain-a-sub-department-id-list-v2">获取子部门编号（/topapi/v2/department/listsubid）</a>
 * @link <a href="https://open.dingtalk.com/document/orgapp/obtain-a-sub-department-id-list-v2">服务端API 通讯录管理 部门管理 获取子部门编号</a>
 * @author feizns
 * @since 2024/6/29
 */
public class ListSubId {

    /**
     * 获取子部门编号
     * @param accessToken 访问令牌
     * @param deptId 部门 ID
     * @return {@link List }<{@link OapiV2DepartmentListsubResponse.DeptBaseResponse}>
     */
    @SneakyThrows
    public static List<Long> execute(String accessToken, Long deptId) {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsubid");
        OapiV2DepartmentListsubidRequest req = new OapiV2DepartmentListsubidRequest();
        req.setDeptId(deptId);
        OapiV2DepartmentListsubidResponse rsp = client.execute(req, accessToken);
        return Utils.ifErrorThrow(rsp).getResult().getDeptIdList();
    }

}
